<?php
class CConsultUtil
{	
	//bitrix/modules/main/tools.php
	function DelDuplicateSort(&$arSort)
	{
		if (is_array($arSort) && count($arSort)>0)
		{
			$arSort2 = array();
			foreach($arSort as $val)
			{
				$arSort1 = explode(" ", trim($val));
				$order = array_pop($arSort1);
				$order_ = strtoupper(trim($order));
				if (!($order_=="DESC" || $order_=="ASC"))
				{
					$arSort1[] = $order;
					$order_ = "";
				}
				$by = implode(" ", $arSort1);
				if(strlen($by)>0 && !array_key_exists($by, $arSort2))
					$arSort2[$by] = $order_;
			}
			$arSort = array();
			foreach($arSort2 as $by=>$order)
				$arSort[] = $by." ".$order;
		}
	}
	//
	function GetFilterOperation($key)
	{
		$strNegative = "N";
		if (substr($key, 0, 1)=="!")
		{
			$key = substr($key, 1);
			$strNegative = "Y";
		}

		if (substr($key, 0, 2)==">=")
		{
			$key = substr($key, 2);
			$strOperation = ">=";
		}
		elseif (substr($key, 0, 1)==">")
		{
			$key = substr($key, 1);
			$strOperation = ">";
		}
		elseif (substr($key, 0, 2)=="<=")
		{
			$key = substr($key, 2);
			$strOperation = "<=";
		}
		elseif (substr($key, 0, 1)=="<")
		{
			$key = substr($key, 1);
			$strOperation = "<";
		}
		elseif (substr($key, 0, 1)=="@")
		{
			$key = substr($key, 1);
			$strOperation = "IN";
		}
		elseif (substr($key, 0, 1)=="%")
		{
			$key = substr($key, 1);
			$strOperation = "LIKE";
		}
		else
		{
			$strOperation = "=";
		}

		return array("FIELD"=>$key, "NEGATIVE"=>$strNegative, "OPERATION"=>$strOperation);
	}
}

class COnline_stream
{
	var $LAST_ERROR="";
	var $METHOD = "";

	function GetByID ($ID)
	{
		global $DB;
		$ID = intval($ID);
		if ($ID <= 0)
			return false;

		$strSql = 	"SELECT C.ID ".
					"FROM b_consult_stream C ".
					"WHERE C.ID='".$ID."'";

		$res = $DB->Query($strSql);
		if($arStream = $res->Fetch())
		{
			return $arStream["ID"];
		}

		return false;

	}

	function GetList($arSort = array("ID"=>"ASC"), $arFilter = array())
	{
		global $DB;
		$arFilter = (is_array($arFilter) ? $arFilter : array());
		$strSqlSearch = " 1=1\n";
		
		foreach($arFilter as $key=>$val)
		{
			if(strlen($val)<=0) continue;
			$val = $DB->ForSql($val);
			switch(strtoupper($key))
			{
				case "ID":
				case "ENTITY_ID":
				case "RESPONSIBLE_ID":
					$strSqlSearch .= " AND U.".$key."='".$val."'\n";
					break;
			}
		}
	
		$strSql = "
			SELECT
				U.*
			FROM
				b_consult_stream U
			WHERE
				".$strSqlSearch."
			";

		list ($by, $order) = $arSort;

		$by = strtolower($by);
		$order = strtolower($order);

		if($by=="id")
			$strSqlOrder = " ORDER BY U.ID ";
		elseif($by == "responsible_id")
			$strSqlOrder = " ORDER BY U.ENTITY_TYPE ";
		elseif($by == "entity_id")
			$strSqlOrder = " ORDER BY U.ENTITY_ID ";
		elseif($by == "date_create")
			$strSqlOrder = " ORDER BY U.DATE_CREATE ";
		else
			$strSqlOrder = " ORDER BY U.ID ";

		if($order=="desc")
			$strSqlOrder .= " desc ";

		$strSql .= $strSqlOrder;
		$res = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
		//echo $strSql."<br>";
		return $res;
	}

	function Delete ($ID)
	{
		global $DB, $USER;
		$ID = intval($ID);
		if ($ID <= 0)
			return false;

		$delmess = true;
		//Удаляем сообщения
		if ($delmess)
			$strSql = 	"DELETE FROM b_consult_stream ".
						"WHERE ID='".$ID."'";
		$res = $DB->Query($strSql);
		if ($res)
			return true;

		return false;
	}

	function Add($arFields)
	{
		global $DB, $USER;

		$this->LAST_ERROR = "";
		$this->METHOD = "COnline_stream::Add(".__LINE__.")";

		if (!COnline_stream::CheckFields($arFields))
			return false;

		$dbInsert = array(
			"RESPONSIBLE_ID" 	=> $arFields["RESPONSIBLE_ID"],
			"~DATE_CREATE" 		=> $DB->GetNowFunction(),
			"ENTITY_ID" 		=> $arFields["ENTITY_ID"],
		);

		$strSql = 	"SELECT C.* ".
					"FROM b_consult_stream C ".
					"WHERE C.ENTITY_ID='".$dbInsert["ENTITY_ID"]."'";

		$res = $DB->Query($strSql);
		if($res->Fetch())
		{
			$strSqlDel = "DELETE FROM b_consult_stream ".
				     "WHERE ENTITY_ID='".$dbInsert["ENTITY_ID"]."'";
			$DB->Query($strSqlDel);
		}

		$arInsert = $DB->PrepareInsert("b_consult_stream", $dbInsert);
		$strSqlInsert =	"INSERT INTO b_consult_stream(".$arInsert[0].") ".
				"VALUES(".$arInsert[1].")";

		if ($DB->Query($strSqlInsert))
			return $DB->LastID();

		return false;
	}
	
	function CheckFields($arFields, $ID = false)
	{
		if (!is_array($arFields))
			return false;

		if ($ID === false)
		{
			if (!isset($arFields["RESPONSIBLE_ID"]))
				$this->LAST_ERROR .= "RESPONSIBLE_ID not set"."<br>";
		}

		//+CHECK ROLES
		if (isset($arFields["RESPONSIBLE_ID"]))
		{
			if (intval($arFields["RESPONSIBLE_ID"]) > 0)
			{
				$arResp = CUser::GetByID($arFields["RESPONSIBLE_ID"]);
				if (!$arResp->Fetch())
					$this->LAST_ERROR .= "RESPONSIBLE_ID contain not user_id"."<br>";
			}
			else
			{
				$this->LAST_ERROR .= "RESPONSIBLE_ID contain incorrect value"."<br>";
			}
		}

		if (isset ($arFields["DATE_CREATE"]) && !CheckDateTime($arFields["DATE_CREATE"]))
			$this->LAST_ERROR .= "DATE_CREATE contain incorrect value"."<br>";

		if(strlen($this->LAST_ERROR)>0)
			return false;

		return true;

	}
}

class COnline_message
{
	var $LAST_ERROR = "";

	function CheckFields($arFields, $ID = false)
	{
		if (!is_array($arFields))
			return false;

		if ($ID === false)
		{
			if (!isset($arFields["STREAM_ID"]))
				$this->LAST_ERROR .= "STREAM_ID not set"."<br>";
			if (!isset($arFields["OWNER_ID"]))
				$this->LAST_ERROR .= "OWNER_ID not set"."<br>";
			if (!isset($arFields["MESSAGE"]))
				$this->LAST_ERROR .= "MESSAGE not set"."<br>";
		}

		if (isset($arFields["STREAM_ID"]))
		{
			$arFields["STREAM_ID"] = intval($arFields["STREAM_ID"]);
			if  ($arFields["STREAM_ID"] > 0)
			{
				$ob_stream = new COnline_stream;
				$DBres = $ob_stream->GetByID ($arFields["STREAM_ID"]);
				if (!$DBres)
					$this->LAST_ERROR .= "STREAM_ID not found"."<br>";
			}
			else
				$this->LAST_ERROR .= "STREAM_ID contain incorrect value"."<br>";

		}

		if (isset($arFields["MESSAGE"]) && strlen(trim($arFields["MESSAGE"])) <= 0)
			$this->LAST_ERROR .= "MESSAGE field is empty"."<br>";

		if (isset($arFields["OWNER_ID"]))
		{
			$arFields["OWNER_ID"] = intval($arFields["OWNER_ID"]);
			if  ($arFields["OWNER_ID"] > 0)
			{
				$ob_stream = new CConsultUser;
				$DBres = $ob_stream->GetList (array(), array("ID" => $arFields["STREAM_ID"]));
				if (!$DBres)
					$this->LAST_ERROR .= "OWNER_ID not found"."<br>";
			}
			else
				$this->LAST_ERROR .= "OWNER_ID contain incorrect value"."<br>";

		}

		if (isset ($arFields["DATE_CREATE"]) && !CheckDateTime($arFields["DATE_CREATE"]))
			$this->LAST_ERROR .= "DATE_CREATE contain incorrect value"."<br>";

		if (isset ($arFields["DATE_READ"]) && !CheckDateTime($arFields["DATE_READ"]))
			$this->LAST_ERROR .= "DATE_READ contain incorrect value"."<br>";

		if(strlen($this->LAST_ERROR)>0)
			return false;
		return true;
	}
	function Update($ID, $arFields)
	{
		global $DB;
		$this->LAST_ERROR = ""; 
		$this->METHOD = "COnline_message::Update(".__LINE__.")";

		$ID = intval($ID);
		
		if(!$this->CheckFields($arFields, $ID))
		{
			return false;
		}
		else
		{
			unset ($arFields["DATE_CREATE"]);
			unset ($arFields["ID"]);
			
			/*foreach ($arFields as $key => $val);
			{
				if (strpos($key, 0, 1) == "=")
				{
					$arFields["~".$key] = $arFields["=".$key];
					unset($arFields["=".$key]);
				}
				
			}*/
			$strUpdate = $DB->PrepareUpdate("b_consult_messages", $arFields);
			$strSql = "UPDATE b_consult_messages SET ".$strUpdate." WHERE ID=".$ID;
			//echo "<pre>".$strSql."</pre>";
			$DBres = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

			if ($DBres) 
				return  true;
			
			return false;

		}
	}
	function Add($arFields)
	{

		global $DB;
		$this->LAST_ERROR = "";
		$this->METHOD = "COnline_message::Add(".__LINE__.")";

		if (!COnline_message::CheckFields($arFields))
			return false;

		$Stream_id = $arFields["STREAM_ID"];
		$strSql = "SELECT max(NUMBER) MAX_NUMBER FROM b_consult_messages WHERE STREAM_ID='$Stream_id'";

		$z = $DB->Query($strSql, false, $err_mess.__LINE__);
		$zr = $z->Fetch();
		$MAX_NUMBER = intval($zr['MAX_NUMBER']);

		$dbInsert = array(
			"STREAM_ID" =>	 $arFields["STREAM_ID"],
			"OWNER_ID" =>	 $arFields["OWNER_ID"],
			"MESSAGE" =>	 $arFields["MESSAGE"],
			"NUMBER" =>	 $MAX_NUMBER + 1,
			"~DATE_CREATE"	=> $DB->GetNowFunction(),
		);

		$arInsert = $DB->PrepareInsert("b_consult_messages", $dbInsert);
		$strSqlInsert =	"INSERT INTO b_consult_messages(".$arInsert[0].") ".
				"VALUES(".$arInsert[1].")";
		if ($DB->Query($strSqlInsert))
			return $DB->LastID();

		return false;
	}

	function GetMessagesForChat($stream_id, $last_id)
	{
		global $DB;
		$stream_id = intval ($stream_id);

		$last_id = intval ($last_id);
		
		$sqlLastID = " AND CM.ID > ".$last_id." ";

		$strSql = "SELECT CM.ID, CM.STREAM_ID, CM.OWNER_ID, CU.ENTITY_ID as REAL_USER_ID, CU.NAME as USER_NAME, CM.MESSAGE, CM.NUMBER, 
				".$DB->DateToCharFunction("CM.DATE_CREATE", "FULL")." as DATE_CREATE
			FROM b_consult_messages CM 
			LEFT JOIN b_consult_user as CU on CM.OWNER_ID = CU.ID AND CU.ENTITY_TYPE = 'U' 
			WHERE 
				1 = 1 AND CM.STREAM_ID = ".$stream_id.$sqlLastID."
			ORDER BY DATE_CREATE ASC";
			//echo $strSql;
		$db_res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		if ($db_res)
		{
			$arRes = array();
			while ($ar = $db_res->Fetch())
				$arRes[] = $ar;
			return $arRes;
		}
		return false;

	}

	function GetList($arOrder = Array("ID"=>"ASC"), $arFilter = array())
	{
		global $DB;
		$arSqlSearch = array();
		$arSqlOrder = array();
		$strSqlSearch = "";
		$strSqlOrder = "";
		$arFilter = (is_array($arFilter) ? $arFilter : array());
		$arAddParams = (is_array($arAddParams) ? $arAddParams : array());
		
		foreach ($arFilter as $key => $val)
		{
			$key_res = CConsultUtil::GetFilterOperation($key);
			$key = strtoupper($key_res["FIELD"]);
			$strNegative = $key_res["NEGATIVE"];
			$strOperation = $key_res["OPERATION"];
			
			switch ($key)
			{
				case "ID":
				case "STREAM_ID":
				case "OWNER_ID":
				case "NUMBER":
					if (($strOperation!="IN") && (intVal($val) > 0))
							$arSqlSearch[] = ($strNegative=="Y"?" CM.".$key." IS NULL OR NOT ":"")."(CM.".$key." ".$strOperation." ".intVal($val)." )";
					elseif (($strOperation =="IN") && ((is_array($val) && count($val)>0 && (array_sum($val) > 0)) || (strlen($val) > 0) ))
					{
						if (is_array($val))
						{
							$val_int = array();
							foreach ($val as $v)
								$val_int[] = intVal($v);
							$val = implode(", ", $val_int);
						}
						$arSqlSearch[] = ($strNegative=="Y"?" NOT ":"")."(CM.".$key." IN (".$DB->ForSql($val).") )";
					}
					else
						$arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(CM.".$key." IS NULL OR CM.".$key."<=0)";
					break;
				case "MESSAGE":
					if (strlen($val)<=0)
						$arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(CM.".$key." IS NULL OR LENGTH(CM.".$key.")<=0)";
					else
						$arSqlSearch[] = ($strNegative=="Y"?" CM.".$key." IS NULL OR NOT ":"")."(CM.".$key." ".$strOperation." '".$DB->ForSql($val)."' )";
					break;
				case "DATE_CREATE":
					if (strlen($val)<=0)
						$arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(CM.".$key." IS NULL OR LENGTH(CM.".$key.")<=0)";
					else
						$arSqlSearch[] = ($strNegative=="Y"?" CM.".$key." IS NULL OR NOT ":"")."(CM.".$key." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "FULL")." )";
					break;
			}
		}
		if (count($arSqlSearch) > 0)
			$strSqlSearch = " AND (".implode(") AND (", $arSqlSearch).") ";
		
		foreach ($arOrder as $by=>$order)
		{
		
			$by = strtoupper($by); $order = strtoupper($order);
			if ($order!="ASC") $order = "DESC";
			if ($by == "STREAM_ID") $arSqlOrder[] = " CM.STREAM_ID ".$order." ";
			elseif ($by == "OWNER_ID") $arSqlOrder[] = " CM.OWNER_ID ".$order." ";
			elseif ($by == "NUMBER") $arSqlOrder[] = " CM.NUMBER ".$order." ";
			elseif ($by == "DATE_CREATE") $arSqlOrder[] = " CM.DATE_CREATE ".$order." ";
			else
			{
				$arSqlOrder[] = " CM.ID ".$order." ";
				$by = "ID";
			}
		}
		$arSqlOrder = array_unique($arSqlOrder);
		DelDuplicateSort($arSqlOrder);
		if(count($arSqlOrder) > 0)
			$strSqlOrder = " ORDER BY ".implode(", ", $arSqlOrder);
			
		$strSql =
			"SELECT CM.ID, CM.STREAM_ID, CM.OWNER_ID, CU.ENTITY_ID as REAL_USER_ID, CU.NAME as USER_NAME, CM.MESSAGE, CM.NUMBER, 
				".$DB->DateToCharFunction("CM.DATE_CREATE", "FULL")." as DATE_CREATE,  
				".$DB->DateToCharFunction("CM.DATE_READ", "FULL")." as DATE_READ 
			FROM b_consult_messages CM
			LEFT JOIN b_consult_user as CU on CM.OWNER_ID = CU.ID AND CU.ENTITY_TYPE = 'U'
			WHERE 1 = 1
			".$strSqlSearch."
			".$strSqlOrder;
		/*if ($iNum > 0 || intVal($arAddParams["nTopCount"]) > 0)
		{
			$iNum = ($iNum > 0) ? $iNum : intVal($arAddParams["nTopCount"]);
			$strSql = "SELECT * FROM(".$strSql.") WHERE ROWNUM<=".$iNum;
		}
		elseif (is_set($arAddParams, "bDescPageNumbering"))
		{
			$iCnt = 0;
			$strSql1 = "SELECT COUNT(FM.ID) as CNT FROM b_forum_message FM WHERE 1 = 1 ".$strSqlSearch;
			$db_res = $DB->Query($strSql1, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			if ($ar_res = $db_res->Fetch())
				$iCnt = intVal($ar_res["CNT"]);
			$db_res =  new CDBResult();
			$db_res->NavQuery($strSql, $iCnt, $arAddParams);
		}
		else
		{*/
			$db_res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		/*}*/
		//echo $strSql;
		return $db_res;		
	}
}


class CConsultUser
{
	var $LAST_ERROR ="";

	function GetDeniedRoleID()
	{
		return "D";
	}

	function GetConsultClientRoleID()
	{
		return "R";
	}

	function GetConsultTeamRoleID()
	{
		return "T";
	}

	function GetAdminRoleID()
	{
		return "W";
	}
	
	// true - если пользователь имеет роль "администратор техподдержки"
	// false - в противном случае
	function IsAdmin($USER_ID=false)
	{
		global $USER;

		if ($USER_ID===false && is_object($USER))
		{
			if ($USER->IsAdmin()) return true;
		}
		return CConsultUser::HaveRole(CConsultUser::GetAdminRoleID(), $USER_ID);
	}
	
	// true - если пользователь имеет роль "сотрудник техподдержки"
	// false - в противном случае
	function IsConsultTeam($USER_ID=false)
	{
		return CConsultUser::HaveRole(CConsultUser::GetConsultTeamRoleID(), $USER_ID);
	}
	
	function IsConsultClient($USER_ID=false)
	{
		return CConsultUser::HaveRole(CConsultUser::GetConsultClientRoleID(), $USER_ID);
	}
	
	function HaveRole($role, $USER_ID=false)
	{
		global $DB, $USER, $APPLICATION, $CONSULT_CACHE_USER_ROLES;
		if (!is_object($USER)) $USER = new CUser;

		if ($USER_ID===false && is_object($USER))
			$UID = $USER->GetID();
		else
			$UID = $USER_ID;

		$arRoles = Array();
		if (array_key_exists($UID, $CONSULT_CACHE_USER_ROLES) && is_array($CONSULT_CACHE_USER_ROLES[$UID]))
		{
			$arRoles = $CONSULT_CACHE_USER_ROLES[$UID];
		}
		else
		{
			$arrGroups = Array();
			if ($USER_ID===false && is_object($USER))
				$arrGroups = $USER->GetUserGroupArray();
			else
				$arrGroups = CUser::GetUserGroup($USER_ID);

			sort($arrGroups);
			$arRoles = $APPLICATION->GetUserRoles("support", $arrGroups);
			$CONSULT_CACHE_USER_ROLES[$UID] = $arRoles;
		}

		if (in_array($role, $arRoles))
			return true;

		return false;
	}
	
	function GetList($arSort = array(), $arFilter = array())
	{
		global $DB;
		$strSqlSearch = " 1=1\n";
		$arFilter = (is_array($arFilter) ? $arFilter : array());
		foreach($arFilter as $key=>$val)
		{
			//if(strlen($val)<=0) continue;
			$val = $DB->ForSql($val);
			switch(strtoupper($key))
			{
				case "ID":
				case "ENTITY_ID":
					$strSqlSearch .= " AND U.".$key."='".$val."'\n";
					break;
				case "ENTITY_TYPE":
					if (strlen($val) == 1)
						$strSqlSearch .= " AND U.ENTITY_TYPE='".$val."'\n";
					break;
			}
		}

		$strSql = "
			SELECT
				U.*
			FROM
				b_consult_user U
			WHERE
				".$strSqlSearch."
			";


		list ($by, $order) = $arSort;

		$by = strtolower($by);
		$order = strtolower($order);

		if($by=="id")
			$strSqlOrder = " ORDER BY U.ID ";
		elseif($by == "entity_type")
			$strSqlOrder = " ORDER BY U.ENTITY_TYPE ";
		elseif($by == "entity_id")
			$strSqlOrder = " ORDER BY U.ENTITY_ID ";
		else
			$strSqlOrder = " ORDER BY U.ID ";

		if($order=="desc")
			$strSqlOrder .= " desc ";

		$strSql .= $strSqlOrder;
		$res = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
		//echo $strSql."<br>";
		return $res;
	}
	
	function Add ($arFields)
	{
		global $DB, $USER, $APPLICATION;

		$this->LAST_ERROR = "";
		$this->METHOD = "CConsultUser::Add(".__LINE__.")";

		if (!$this->CheckFields($arFields))
			return false;

		$bAuth = $USER->IsAuthorized();

		$dbInsert = array(
			"~DATE_CREATE" => (isset($arFields ["DATE_CREATE"])? $arFields ["DATE_CREATE"] : $DB->GetNowFunction()),
			"ENTITY_TYPE" => $arFields["ENTITY_TYPE"],
			"ENTITY_ID" => $arFields["ENTITY_ID"],
			"NAME" => $arFields["NAME"],
		);

		//Check exists user
		$strSql = 	"SELECT 'x'
					FROM b_consult_user
					WHERE ENTITY_TYPE = '".$DB->ForSQL ($arFields["ENTITY_TYPE"], 1)."' AND ENTITY_ID = '".$DB->ForSQL ($arFields["ENTITY_ID"], 32)."'";

		$res = $DB->Query($strSql);
		if ($res->Fetch())
		{
			$this->LAST_ERROR .= "User already exists<br>";
			return false;
		}
		//END Check exists user
		$arInsert = $DB->PrepareInsert("b_consult_user", $dbInsert);
		$strSqlInsert =	"INSERT INTO b_consult_user(".$arInsert[0].") ".
				"VALUES(".$arInsert[1].")";

		if ($DB->Query($strSqlInsert))
		{
			if (($arFields["ENTITY_TYPE"] != "U"))
				$APPLICATION->set_cookie("CONSULT_USER_SID", $arFields["ENTITY_ID"], time()+60*60*24*30, "/");

			$ID = $DB->LastID();
			return $ID;
		}

		return false;

	}

	function CheckFields ($arFields, $ID = false)
	{
		if (!is_array($arFields))
			return false;

		if ($ID === false)
		{
			if (!isset($arFields["ENTITY_TYPE"]) || !isset($arFields["ENTITY_ID"]))

				$this->LAST_ERROR .= "ENTITY_TYPE or ENTITY_ID can't be empty"."<br>";
		}
		if (isset($arFields["ENTITY_ID"]) && $arFields["ENTITY_TYPE"] == "U")
		{
			if (intval($arFields["ENTITY_ID"]) > 0)
			{
				$arResp = CUser::GetByID($arFields["ENTITY_ID"]);
				if (!$arResp->Fetch())
					$this->LAST_ERROR .= "ENTITY_ID contain not ID of user"."<br>";
			}
			else
			{
				$this->LAST_ERROR .= "ENTITY_ID contain incorrect value"."<br>";
			}
		}

		/*if (isset ($arFields["DATE_CREATE"]) && !CheckDateTime($arFields["DATE_CREATE"]))
			$this->LAST_ERROR .= "DATE_CREATE contain incorrect value"."<br>";*/

		if(strlen($this->LAST_ERROR)>0)
			return false;

		return true;
	}
	function Update($ID, $arFields)
	{
		global $DB;
		$this->LAST_ERROR = ""; 
		$this->METHOD = "CConsultUser::Update(".__LINE__.")";

		$ID = intval($ID);
		
		if(!$this->CheckFields($arFields, $ID))
		{
			return false;
		}
		else
		{
			unset ($arFields["DATE_CREATE"]);
			unset ($arFields["ID"]);

			$strUpdate = $DB->PrepareUpdate("b_consult_user", $arFields);
			$strSql = "UPDATE b_consult_user SET ".$strUpdate." WHERE ID=".$ID;
			echo "<pre>".$strSql."</pre>";
			$DBres = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

			if ($DBres) 
				return  true;
			
			return false;

		}
	}
	function Delete ($ID)
	{
		global $DB, $USER;
		$ID = intval($ID);
		if ($ID <= 0)
			return false;

		$strSql = "DELETE FROM b_consult_user ".
						"WHERE ID='".$ID."'";
		$res = $DB->Query($strSql);
		if ($res)
			return true;

		return false;
	}
}
?>
